<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>客户数据导入</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .upload-area {
            border: 2px dashed #ddd;
            padding: 40px;
            text-align: center;
            border-radius: 8px;
            margin: 20px 0;
        }
        .upload-area:hover {
            border-color: #007bff;
        }
        input[type="file"] {
            margin: 10px 0;
        }
        .btn {
            background-color: #007bff;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .format-info {
            background-color: #e7f3ff;
            padding: 15px;
            border-radius: 4px;
            margin: 20px 0;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>客户数据批量导入</h1>
        
        <div class="format-info">
            <h3>Excel格式要求：</h3>
            <p><strong>第一行为表头，从第二行开始为数据</strong></p>
            <p>列顺序：客户名称 | 联系电话 | 联系邮箱 | 地址 | 客户等级 | 行业类型 | 客户来源 | 状态 | 备注</p>
            <p><strong>必填：</strong>客户名称</p>
            <p><strong>状态：</strong>1-活跃, 0-非活跃 (默认1)</p>
        </div>
        
        <div class="upload-area">
            <input type="file" id="fileInput" accept=".xlsx,.xls" style="display: none;">
            <button onclick="document.getElementById('fileInput').click()" class="btn">选择Excel文件</button>
            <p id="fileName" style="margin-top: 10px; color: #666;"></p>
        </div>
        
        <div style="text-align: center;">
            <button onclick="importData()" class="btn" id="importBtn" disabled>开始导入</button>
        </div>
        
        <div id="result"></div>
    </div>

    <script>
        document.getElementById('fileInput').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                document.getElementById('fileName').textContent = '已选择: ' + file.name;
                document.getElementById('importBtn').disabled = false;
            }
        });

        async function importData() {
            const fileInput = document.getElementById('fileInput');
            const resultDiv = document.getElementById('result');
            const importBtn = document.getElementById('importBtn');
            
            if (!fileInput.files[0]) {
                alert('请选择文件');
                return;
            }
            
            const formData = new FormData();
            formData.append('file', fileInput.files[0]);
            
            importBtn.disabled = true;
            importBtn.textContent = '导入中...';
            
            try {
                const response = await fetch('/api/customer/import', {
                    method: 'POST',
                    body: formData
                });
                
                const result = await response.json();
                
                if (result.success) {
                    let html = `<div class="result success">
                        <h3>导入成功！</h3>
                        <p>成功导入：${result.successCount} 条记录</p>
                        <p>错误数量：${result.errorCount} 条</p>
                    </div>`;
                    
                    if (result.errors && result.errors.length > 0) {
                        html += `<div class="result error">
                            <h4>错误详情：</h4>
                            <ul>${result.errors.map(error => '<li>' + error + '</li>').join('')}</ul>
                        </div>`;
                    }
                    
                    resultDiv.innerHTML = html;
                } else {
                    resultDiv.innerHTML = `<div class="result error">导入失败：${result.message}</div>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<div class="result error">导入失败：${error.message}</div>`;
            } finally {
                importBtn.disabled = false;
                importBtn.textContent = '开始导入';
            }
        }
    </script>
</body>
</html> 